package com.diancan.web.controller.dc;

import java.util.Date;
import java.util.List;

import com.diancan.common.utils.ShiroUtils;
import com.diancan.common.utils.uuid.IdUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.diancan.common.annotation.Log;
import com.diancan.common.enums.BusinessType;
import com.diancan.dc.domain.DcOrderInfo;
import com.diancan.dc.service.IDcOrderInfoService;
import com.diancan.common.core.controller.BaseController;
import com.diancan.common.core.domain.AjaxResult;
import com.diancan.common.utils.poi.ExcelUtil;
import com.diancan.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author dc
 * @date 2021-12-27
 */
@Controller
@RequestMapping("/dc/orderInfo")
public class DcOrderInfoController extends BaseController {
    private String prefix = "dc/orderInfo";

    @Autowired
    private IDcOrderInfoService dcOrderInfoService;

    @RequiresPermissions("dc:orderInfo:view")
    @GetMapping()
    public String orderInfo() {
        return prefix + "/orderInfo";
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("dc:orderInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DcOrderInfo dcOrderInfo) {
        String loginName = ShiroUtils.getLoginName();
        if (!"admin".equals(loginName)) {
            dcOrderInfo.setUserName(ShiroUtils.getLoginName());
        }
        startPage();
        List<DcOrderInfo> list = dcOrderInfoService.selectDcOrderInfoList(dcOrderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("dc:orderInfo:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DcOrderInfo dcOrderInfo) {
        List<DcOrderInfo> list = dcOrderInfoService.selectDcOrderInfoList(dcOrderInfo);
        ExcelUtil<DcOrderInfo> util = new ExcelUtil<DcOrderInfo>(DcOrderInfo.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("dc:orderInfo:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DcOrderInfo dcOrderInfo) {
        dcOrderInfo.setId(IdUtils.simpleUUID());
        dcOrderInfo.setCreateTime(new Date());
        return toAjax(dcOrderInfoService.insertDcOrderInfo(dcOrderInfo));
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("dc:orderInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        DcOrderInfo dcOrderInfo = dcOrderInfoService.selectDcOrderInfoById(id);
        mmap.put("dcOrderInfo", dcOrderInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("dc:orderInfo:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DcOrderInfo dcOrderInfo) {
        return toAjax(dcOrderInfoService.updateDcOrderInfo(dcOrderInfo));
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("dc:orderInfo:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dcOrderInfoService.deleteDcOrderInfoByIds(ids));
    }
}
